﻿using System;
using System.CodeDom.Compiler;
using System.Web;
using System.Web.Mvc;
using System.Web.Compilation;
using System.Globalization;
using System.Text;
using System.Web.Mvc.Html;

namespace DiplomaWeb.Helpers
{
    public static class LocalizationHelpers
    {
        /// <summary>
        /// Load resource string
        /// </summary>
        /// <param name="htmlhelper">html helper object</param>
        /// <param name="expression">resorce key expression</param>
        /// <param name="args">string arguments</param>
        /// <returns>result resource string</returns>
        public static MvcHtmlString Resource(this HtmlHelper htmlhelper, string expression, params object[] args)
        {
            string virtualPath = GetVirtualPath(htmlhelper);
            return GetResourceString(htmlhelper.ViewContext.HttpContext, expression, virtualPath, args);
        }

        /// <summary>
        /// Load resource string
        /// </summary>
        /// <param name="controller">controller object</param>
        /// <param name="expression">resorce key expression</param>
        /// <param name="args">string arguments</param>
        /// <returns>result resource string</returns>
        public static MvcHtmlString Resource(this Controller controller, string expression, params object[] args)
        {
            return GetResourceString(controller.HttpContext, expression, "~/", args);
        }

        /// <summary>
        /// Load resource string for control (partial view)
        /// </summary>
        /// <param name="htmlHelper">html helper object</param>
        /// <param name="expression">resorce key expression</param>
        /// <param name="virtualPath">virtual path string</param>
        /// <param name="args">string arguments</param>
        /// <returns>result resource string</returns>
        public static MvcHtmlString ResourceCtrl(this HtmlHelper htmlHelper, string expression, string virtualPath, params object[] args)
        {
            return GetResourceString(htmlHelper.ViewContext.HttpContext, expression, virtualPath, args);
        }

        private static MvcHtmlString GetResourceString(HttpContextBase httpContext, string expression, string virtualPath, object[] args)
        {
            try
            {
                var curCult = CultureInfo.CurrentUICulture;
                var invCult = CultureInfo.InvariantCulture;
                var context = new ExpressionBuilderContext(virtualPath);
                var builder = new ResourceExpressionBuilder();
                var fields = (ResourceExpressionFields)builder.ParseExpression(expression, typeof(string), context);

                try
                {
                    var fmtStr = "";

                    if (!string.IsNullOrEmpty(fields.ClassKey))
                    {
                    	fmtStr = (string)httpContext.GetGlobalResourceObject(fields.ClassKey, fields.ResourceKey, curCult);
                    }
                    else
                    {
                    	fmtStr = (string)httpContext.GetLocalResourceObject(virtualPath, fields.ResourceKey, curCult);
                    }

                    if (string.IsNullOrEmpty(fmtStr) && curCult != invCult)
					{
						if (!string.IsNullOrEmpty(fields.ClassKey))
                        {
                        	fmtStr = (string)httpContext.GetGlobalResourceObject(fields.ClassKey, fields.ResourceKey, invCult);
                        }
                        else
                        {
                        	fmtStr = (string)httpContext.GetLocalResourceObject(virtualPath, fields.ResourceKey, invCult);
                        }
					}

                    if (string.IsNullOrEmpty(fmtStr)) throw new Exception();

                    return MvcHtmlString.Create(string.Format(fmtStr, args));
                }
                catch
                {
                    return MvcHtmlString.Create(fields.ResourceKey);
                }
            }
            catch(Exception ex)
            {
                return MvcHtmlString.Create(expression);
            }
        }

        private static string GetVirtualPath(HtmlHelper htmlHelper)
        {
            string virtualPath = null;
            var view = htmlHelper.ViewContext.View as BuildManagerCompiledView;

            if (view != null)
            {
                virtualPath = view.ViewPath;
            }
            return virtualPath;
        }

        private static string ToScriptSafeString(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            s = s.Replace(@"\", @"\\");
            s = s.Replace("\"", "\\\"");
            s = s.Replace("'", @"\'");
            s = s.Replace("\r", @"\r");
            s = s.Replace("\n", @"\n");
            s = s.Replace("\t", @"\t");
            return s;
        }
    }
}